#! /bin/bash        

#set path for cron
export PATH=/ffp/sbin:/usr/sbin:/sbin:/ffp/bin:/usr/bin:/bin

count=0
feed=feedList.txt
feedFolder="feeds"
watchFolder="../watcher"

# loop through the feed list
while read line        
do      
	let count++  
    	echo -e  "$count - $line"
        
	#look for saved file
	if [ ! `ls ./$feedFolder/$count.saved` ]; 
	then 
		#make it
		echo > ./$feedFolder/$count.saved
		echo "$count.saved doesn't exist, now creating" 
	fi

	#update feed
	wget -O ./$feedFolder/$count.rss $line

	#get the diff and wget the torrents
	diff ./$feedFolder/$count.rss ./$feedFolder/$count.saved | grep -o '<link>[^"]*' | sed 's/<link>//g' | sed 's/<\/link>//g' | xargs wget -c
	#diff ./$feedFolder/$count.rss ./$feedFolder/$count.saved | grep -o '<link>[^"]*' | sed 's/<link>//g' | sed 's/<\/link>//g'
	
	#move to watchfolder for transmission
	mv *.torrent $watchFolder
	
	#chown so that transmission can use those torrents
	chown -R nobody:501 $watchFolder/*

	#save the need feed for next run
	cp ./$feedFolder/$count.rss ./$feedFolder/$count.saved
done <$feed  

